﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entities;

namespace Presentation
{
    public partial class InputPIN : System.Web.UI.Page
    {
        CardsEntity cE = new CardsEntity();
        CardBLL cB = new CardBLL();
        CardReaderBLL cRB = new CardReaderBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.Request.Params["id"] == "1")
                {
                    lbPass.Text = "Wrong PIN, Please try again";
                }
            }
        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "1";
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "2";
        }

        protected void btn3_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "3";
        }

        protected void btn4_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "4";
        }

        protected void btn5_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "5";
        }

        protected void btn6_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "6";
        }

        protected void btn7_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "7";
        }

        protected void btn8_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "8";
        }

        protected void btn9_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "9";
        }

        protected void btn0_Click(object sender, EventArgs e)
        {
            txtPIN.Text += "0";
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtPIN.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://localhost:2439/InputCardNo.aspx");
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            cE.CardNo = Session["CardNo"].ToString();
            cE.PIN = txtPIN.Text;
            bool ValiPIN = cRB.ValidatePIN(cE);
            int Attempt = cB.GetAttempt(cE);
            if (Attempt >= 3)
            {
                cRB.SwallowCard(cE);
                Response.Redirect("http://localhost:2439/Screen.aspx/?id=5");
            }
            else
            {
                if (ValiPIN == true)
                {
                    //Add session AccountID
                    Session.Add("AccountID", cB.GetAccountID(cE));
                    cB.UnBlock(cE);
                    Response.Redirect("http://localhost:2439/Screen.aspx/?id=4");
                }
                else
                {
                    Response.Redirect("http://localhost:2439/Screen.aspx/?id=6");
                }
            }
        }
    }
}